﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;

namespace Onshi.POS.Modelo.Objectos
{
    public class ManejoImagen
    {
        #region Metodos

        public static Byte[] ImagenAByte(Image _imagen)
        {
            if (_imagen != null)
            {
                String sTemp = Path.GetTempFileName();
                FileStream fs = new FileStream(sTemp, FileMode.OpenOrCreate, FileAccess.ReadWrite);
                _imagen.Save(fs, ImageFormat.Png);
                fs.Position = 0;
                Int32 imgLength = Convert.ToInt32(fs.Length);
                Byte[] bytes = new Byte[imgLength];
                fs.Read(bytes, 0, imgLength);
                fs.Close();
                return bytes;
            }
            else
            {
                return null;
            }
        }

        public static Image ByteAImagen(Byte[] _bytes)
        {
            try
            {
                if (_bytes == null)
                    return null;
                MemoryStream ms = new MemoryStream(_bytes);
                Bitmap bm = null;
                bm = new Bitmap(ms);
                return bm;
            }
            catch (Exception)
            {
                return null;
            }
        }

        #endregion
    }
}
